const { src, dest, parallel, series } = require("gulp");
const gulpSass = require("gulp-sass")(require("node-sass"));
const gulpBabel = require("gulp-babel");
const gulpSwig = require("gulp-swig");
const gulpImagemin = require("gulp-imagemin");
const del = require("del");

//删除dist
//cnpm i del --save-dev
const clean = () => {
  return del(["dist"]);
};

// scss样式编译
const style = () => {
  return src("src/assets/styles/*.scss", { base: "src" })
    .pipe(gulpSass({ outputStyle: "expanded" }))
    .pipe(dest("dist"));
};

//es6脚本编译,cnpm i @babel/core --save-dev ,cnpm i @babel/preset-env --save-dev
const script = () => {
  return src("src/assets/scripts/*.js", { base: "src" })
    .pipe(gulpBabel({ presets: ["@babel/preset-env"] }))
    .pipe(dest("dist"));
};

//页面swig模板编译
//cnpm install gulp-swig --save-dev
let data = {
  title: "我在北京天安门",
  date: new Date(),
};
const page = () => {
  return src("src/assets/swig/*.html", { base: "src" })
    .pipe(gulpSwig({ data }))
    .pipe(dest("dist"));
};

//图片与字体文件转换
//cnpm install gulp-imagemin --save-dev
const image = () => {
  return src("src/assets/images/**", { base: "src" })
    .pipe(gulpImagemin())
    .pipe(dest("dist"));
};
const font = () => {
  return src("src/assets/fonts/**", { base: "src" })
    .pipe(gulpImagemin())
    .pipe(dest("dist"));
};

//其他文件处理
const extra = () => {
  return src("public/**", { base: "public" }).pipe(dest("dist"));
};

const compile = parallel(style, script, page);
const build = series(clean, parallel(compile, extra));

module.exports = {
  style,
  script,
  page,
  image,
  extra,
  clean,
  compile,
  build,
};
